﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XMLGener.Interfaces;
using XMLGener.Data;
namespace XBasicModules
{
    /// <summary>
    /// Generetor of random numbers
    /// </summary>
    public class RandomNumber :ModuleBase, XMLGenerModuleInterface
    {

        private Random rnd = new Random();
        private int minNum = 0;
        private int maxNum = 1000;
        #region XMLGenerModuleInterface Members

        /// <summary>
        /// Return next random number;
        /// </summary>
        /// <returns></returns>
        public virtual string generate()
        {
           if (rnd==null) return "";
           return  Convert.ToString(rnd.Next(minNum,maxNum));
        }

        /// <summary>
        /// Retrurn definitnion of this module
        /// </summary>
        /// <returns>xml data</returns>
        public virtual string getDefinition()
        {
            return
              "<function>"+
              "   <name>Random number</name>"+
              "   <id>randomNumber</id>"+
              "   <type>number</type>"+
              "   <attributes>"+
              "      <attribute>"+
              "         <name>Minimum value</name>"+
              "         <id>minnum</id>"+
              "         <type>number</type>"+
              "      </attribute>"+
              "      <attribute>"+
              "         <name>Maximum value</name>"+
              "         <id>maxnum</id>"+
              "         <type>number</type>"+
              "      </attribute>"+
              "   </attributes>"+
              "</function>";
        }

        /// <summary>
        /// Set attributes for this module before generating.
        /// </summary>
        /// <param name="xmlParams"></param>
        /// <param name="parentDocument"></param>
        public virtual void setParameters(string xmlParams, XMLGenerDocument parentDocument)
        {
            if (!this.checkSettingsElement(xmlParams, parentDocument))
                return;
            this.minNum = Convert.ToInt32(this.getParam("minnum"));
            this.maxNum = Convert.ToInt32(this.getParam("maxnum"));
        }

        #endregion
    }
}
